#include "mybutton.h"

MyButton::MyButton(QString pressImage,
                   QString releaseImage,
                   QWidget *parent) :
    QPushButton(parent),
    mPressImage(pressImage),
    mReleaseImage(releaseImage)
{
    //初始化鼠标状态为放开
    this->stat = Release;
}

void MyButton::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    QPixmap pm;
    if(stat == Press){
        pm.load(mPressImage);
    }else if(stat == Release){
        pm.load(mReleaseImage);
    }
    //绘制图片
    p.drawPixmap(0, 0, this->width(), this->height(), pm);
    //绘制文字
    p.drawText(0, 0, this->width(), this->height(),
               Qt::AlignHCenter | Qt::AlignVCenter, this->text());
}

void MyButton::buttonUp()
{
    //向上动画效果
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry",this);
    //设置起始位置
    animation->setStartValue(this->geometry());
    //设置移动后的位置
    animation->setEndValue(QRect(this->x(), this->y() - 10, this->width(), this->height()));
    //设置速度
    animation->setDuration(100);
    //开始， 完成后自动释放内存
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

void MyButton::buttonDown()
{
    //向下动画效果
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry",this);
    //设置起始位置
    animation->setStartValue(this->geometry());
    //设置移动后的位置
    animation->setEndValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));
    //设置速度
    animation->setDuration(100);
    //开始， 完成后自动释放内存
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

void MyButton::mousePressEvent(QMouseEvent *e)
{
    //设置鼠标的状态
    this->stat = Press;
    //更新画图
    update();
    QPushButton::mousePressEvent(e);
}

void MyButton::mouseReleaseEvent(QMouseEvent *e)
{
    //设置鼠标的状态
    this->stat = Release;
    //更新画图
    update();
    QPushButton::mouseReleaseEvent(e);
}
